<?php
namespace Admin\Controller;

use Util\AccessController;

class ArticlesController extends AccessController {

    public function Index() {
        $LS = Service('M','articles');
        $count = $LS->count();
        $Page = new \Think\NewPage($count, 10);
        $show = $Page->show(); // 分页显示输出
        $limit = $Page->limit();
        $this->show = $show;
        $where = array();
        $files = '*';
        $order = 'CREATE_TIME desc';
        $data = $LS->query($where,$limit,$files,$order);
        $this->DATA =$data;
        $this->display();
    }

    //由isbn获取书籍
    public function GetBookToISBN(){
        $LS =Service('M','book');
        $where['ISBN'] = $_POST['ISBN'];
        $order = 'SORT asc';
        $data = $LS->query($where,null,null,$order);
        $this->ajaxReturn($data,'JSON');

    }

    public function AddBookToArticles(){
        $LS = Service('M','articles');
        $where['ID'] =$_POST['A_ID'];
        $save['BOOK_ID'] =$_POST['ID'];
        $OK = $LS->update($where,$save);
        echo $OK;
    }

    public function update() {
        if (IS_POST) {
            $us = Service('User', 'user');
            $data = I("post.");
            if ($us->update($data)) {
                $vo['info'] = "修改成功！";
                $vo['state'] = 1;
            } else {
                $vo['info'] = "修改失败！";
                $vo['state'] = 0;
            }
            $this->ajaxReturn($vo);
        }
    }

    public function EditAddIndex() {
        $LS = Service('Articles','articles');
        $data = $LS->find(array('ID'=>$_GET['ID']));
        if($data['BOOK_ID']){
            $BOOK_LS = Service('M','book');
            $BookData = $BOOK_LS->find(array('ID'=>$data['BOOK_ID']));
            $this->BookData = $BookData;
        }
        $this->ID = $_GET['ID'];
        $this->DATA =$data;
        $this->display();
    }

    /*
     * 编辑添加文章
     */
    public function EditAdd() {
        $LS = Service('M', 'articles');
        if ($_POST['ID']) {
            $where['ID'] = $_POST['ID'];
            $save['TITLE'] = $_POST['TITLE'];
            $save['DATA'] = preg_replace("/<img\s+[^>]*?src=(\'|\")(.*?)\\1[^>]*>/i", '<img src="$2" class="img-responsive center-block"/>', $_POST['CONTENT']);
            $OK = $LS->update($where,$save);
            $ID = $_POST['ID'];
        } else {
            $save = $_POST;
            $ID = 'A_' . uniqid() . rand();
            $save['ID'] = $ID;
            $save['USER_ID'] = $this->S('ID');
            $save['CREATE_TIME'] = date('Y-m-d H:i:s');
            $save['SOURCE'] = '奇妙阅平台';
            $save['USER_NAME'] = '奇妙阅平台';
            $save['USER_PIC'] = 'http://www.yanxiang.pw/Book/qmy.jpg';
            $save['DATA'] = preg_replace("/<img\s+[^>]*?src=(\'|\")(.*?)\\1[^>]*>/i", '<img src="$2" class="img-responsive center-block"/>', $_POST['CONTENT']);
            $OK = $LS->Add($save);
        }
        $OK? $this->success('编辑成功',U('Articles/EditAddIndex?ID='.$ID)):  $this->error('编辑失败');

    }
    /*
     * 删除
     */
    public function Del(){
        $LS = Service('M','articles');
        $OK = $LS->Del(array('ID'=>$_POST['ID']));
        echo $OK;
    }

}